package org.jeeasy.system.modules.user.domain.model;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.jeeasy.common.core.domain.IAuthUser;
import org.jeeasy.system.enums.user.SysUserStatusEnum;
import org.jeeasy.system.modules.dept.domain.SysDept;
import org.jeeasy.system.modules.user.domain.vo.SysUserVo;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author Alps
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
public class SystemAuthUser extends SysUserVo implements IAuthUser {

    @JsonIgnore
    @Schema(description = "密码")
    private String password;

    private String currDeptCode;

    private Collection<String> roleSet;
    private Collection<String> permissionSet;

    @Override
    public String id() {
        return this.getId();
    }

    @Override
    public String password() {
        return this.getPassword();
    }

    @Override
    public String username() {
        return this.getUsername();
    }

    @Override
    public String deptCode() {
        if (StrUtil.isNotBlank(currDeptCode)) {
            return currDeptCode;
        } else {
            List<SysDept> depts = super.getDepts();
            if (!CollectionUtils.isEmpty(depts)) {
                return Optional.ofNullable(depts.get(0))
                        .map(SysDept::getOrgCode)
                        .orElse(null);
            }
        }
        return null;
    }


    @Override
    public boolean izAccountNonExpired() {
        return IAuthUser.super.izAccountNonExpired();
    }

    @Override
    public boolean izAccountNonLocked() {
        return false;
    }

    @Override
    public boolean izCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean izEnabled() {
        return SysUserStatusEnum.NORMAL.getValue().equals(this.getStatus());
    }

}
